﻿using System;
using System.Linq;
using Foxit.PDF;
using Foxit.PDF.PageElements;
using Sustainalytics.Reporting.Framework;

namespace Sustainalytics.ReportGenerator.BL
{
    public class TearSheetKeyIssuesSection : PdfReportSectionGenerator
    {
        private const float _sectionNameFontSize = 12f;

        public override void ComposeSectionContent()
        {
            const float startPosition = 130f;

            var page = _report.NewPage();
            _report.Pages.Add(page);


            AddHeaderToPage(page);

            AddKeyIssues(page, startPosition);
            

            _outlinePositions.Add(new Reporting.Framework.OutlinePosition { Name = "Key Issues", PageNumber = 1 });
        }

        private void AddKeyIssues(Page page, float top)
        {
            var left = _report.LeftMargin;



            const float helpTextWidth = 180f;
            const float helpTextHeight = 300f;
            const float emptyRowHeight = 5f;

            var tableWidth = _report.PageMaxWidth - helpTextWidth - 20f;
            var tableHeight = _report.PageMaxHeight - top;

            const string sectionName = "<p><b>MANAGEMENT OF KEY ESG ISSUES</b></p>";
            const string helpText = "<p><font color='#000000'>A Key ESG Issue is an issue that is most material to a company from a business and sustainability perspective. The below assessments of Key ESG Issues take into account the company’s exposure as well as its preparedness to manage these issues effectively.</font></p>";

            var style = new FormattedTextAreaStyle(Fonts.CalibriLightFontFamily, 6.5f, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 0, AllowOrphanLines = false }
            };

            page.Elements.Add(new FormattedTextArea(sectionName, left, top, helpTextWidth, _sectionNameFontSize + 2f, Fonts.CalibriFontFamily, _sectionNameFontSize, false));


            page.Elements.Add(new Rectangle(left + helpTextWidth + 20f, top, tableWidth, 45f, new RgbColor(240, 240, 240), new RgbColor(240, 240, 240)));
            page.Elements.Add(new FormattedTextArea(helpText, left + helpTextWidth + 40f, top + 10f, tableWidth - 50f, helpTextHeight, style));

            top += 60;

            style = new FormattedTextAreaStyle(Fonts.CalibriLightFontFamily, 8f, false)
            {
                Paragraph = { Align = TextAlign.Justify, Indent = 0, SpacingAfter = 0, AllowOrphanLines = false }
            };

            if (_dataSource.KeyEsgIssues.Any())
            {
                const float titleHeight = 15f;
                var totalHeight = 0.0f;

                var table = GetDefaultTable(left + helpTextWidth + 20f, top, tableWidth, tableHeight);

                table.Columns.Add(20f);
                table.Columns.Add(tableWidth - 20f);

                foreach (var keyEsgIssue in _dataSource.KeyEsgIssues)
                {
                    var mei = _dataSource.RiskRatingResearchProfile.MaterialEsgIssues
                        .FirstOrDefault(x => x.Name.ToLower() == keyEsgIssue.Name.Value.ToLower());

                    if (mei != null && mei.ManagementComment != null)
                    {
                        var managementComment = string.IsNullOrEmpty(mei.ManagementComment.Value)
                            ? string.Empty
                            : mei.ManagementComment.Value;

                        var keyEsgIssueText = string.Format(
                            "<p SpacingAfter=5><font color='#000000' PointSize=12><b>{0}</b></font></p><p>{1}</p>",
                            mei.Name, managementComment);

                        var formattedTextArea = new FormattedTextArea(keyEsgIssueText, 0.0f, 0.0f, tableWidth - 20f, 500f, style);
                        var textHeight = (float)Math.Round((decimal)formattedTextArea.GetRequiredHeight(), 0, MidpointRounding.AwayFromZero) + 15f;

                        formattedTextArea = new FormattedTextArea(keyEsgIssueText, 0.0f, 0.0f, tableWidth - 20f, textHeight, style);

                        var row = table.Rows.Add(textHeight);
                        row.Cells.Add(new Circle(5f, 5f, 4f, new RgbColor(20, 167, 157), RgbColor.White, 2f, LineStyle.Solid));
                        row.Cells.Add(formattedTextArea);

                        totalHeight += titleHeight + textHeight + emptyRowHeight;
                    }
                }

                page.Elements.Add(table);
            }
        }
    }
}
